<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图片编辑任务状态</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .status-info {
            margin-bottom: 20px;
            padding: 15px;
            border-radius: 5px;
        }
        .pending {
            background-color: #fff3cd;
            border: 1px solid #ffeaa7;
            color: #856404;
        }
        .running {
            background-color: #d1ecf1;
            border: 1px solid #bee5eb;
            color: #0c5460;
        }
        .completed {
            background-color: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .failed {
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        .log-container {
            background-color: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 5px;
            padding: 15px;
            height: 300px;
            overflow-y: auto;
            font-family: monospace;
            white-space: pre-wrap;
        }
        .back-link {
            display: inline-block;
            margin-top: 20px;
            color: #007bff;
            text-decoration: none;
        }
        .back-link:hover {
            text-decoration: underline;
        }
        .result-image {
            max-width: 100%;
            margin-top: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>图片编辑任务状态</h1>
        
        <div id="taskInfo" class="status-info {{ task.status }}">
            <h3>任务 #{{ task.id }}</h3>
            <p><strong>状态:</strong> <span id="status">{{ task.status }}</span></p>
            <p><strong>消息:</strong> <span id="message">{{ task.message }}</span></p>
            {% if task.prompt %}
            <p><strong>编辑描述:</strong> {{ task.prompt }}</p>
            {% endif %}
            {% if task.negative_prompt %}
            <p><strong>负面描述:</strong> {{ task.negative_prompt }}</p>
            {% endif %}
        </div>
        
        {% if task.status == 'completed' and task.output_path %}
        <div>
            <h3>编辑结果:</h3>
            <img src="{{ task.output_path }}" alt="编辑后的图片" class="result-image">
            <p><a href="{{ task.output_path }}" target="_blank">查看原图</a></p>
        </div>
        {% endif %}
        
        <a href="{{ url_for('image_edit') }}" class="back-link">← 返回图片编辑</a>
    </div>

    <script>
        // 每2秒轮询一次任务状态
        setInterval(function() {
            fetch('/api/image-edit-task/{{ task.id }}')
                .then(response => response.json())
                .then(data => {
                    // 更新状态信息
                    document.getElementById('status').textContent = data.status;
                    document.getElementById('message').textContent = data.message;
                    
                    // 更新状态类
                    const taskInfo = document.getElementById('taskInfo');
                    taskInfo.className = 'status-info ' + data.status;
                    
                    // 如果任务完成，刷新页面以显示结果
                    if (data.status === 'completed' || data.status === 'failed') {
                        window.location.reload();
                    }
                })
                .catch(error => {
                    console.error('获取任务状态失败:', error);
                });
        }, 2000);
    </script>
</body>
</html>